package com.nut.esdemo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nut.esdemo.common.Constants;
import com.nut.esdemo.common.HttpResult;
import com.nut.esdemo.entity.Data;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Crawl {

    private static final HttpResult doGet(String url) {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.addRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36");

            int respCode = conn.getResponseCode();
            String resp = null;
            if (HttpURLConnection.HTTP_OK == respCode) {
                resp = IoUtils.toString(conn.getInputStream(), Constants.ENCODE);
            } else {
                resp = IoUtils.toString(conn.getErrorStream(), Constants.ENCODE);
            }
            return new HttpResult(respCode, conn.getHeaderFields(), resp);
        } catch (Throwable e) {
            return null;
        } finally {
            IoUtils.closeQuietly(conn);
        }
    }

    public static final String url = "http://water.nbyz.cn/?ft=2";

    private static List<Data> getData() {

        HttpResult httpResult = doGet(url);
        if (httpResult == null) {
            System.out.println("httpresult=null");
            return null;
        }
        Document document = Jsoup.parse(httpResult.getContent());
        Elements elements = document.getElementsByTag("script");

        Element element = elements.get(8);
        String html = element.html();
        System.out.println("html=" + html);

        int i = html.indexOf("loadData=");
        String substring = html.substring(i + 9);
        int idx = substring.indexOf(";");
        String json = substring.substring(0, idx);
        JSONObject jsonObject = JSON.parseObject(json);
        JSONArray rowData = jsonObject.getJSONArray("rowData");

        int size = rowData.size();
        ArrayList<Data> list = new ArrayList<>();
        for (int k = 0; k < size; k++) {
            JSONObject jsonObject1 = rowData.getJSONObject(k);
            JSONArray array = jsonObject1.getJSONArray("data");

            Data data = new Data();
            data.setName(array.get(0).toString());
            data.setValue(array.get(1).toString());
            list.add(data);
        }

        return list;
    }

    public static void main(String[] args) {
        List<Data> data = getData();
        System.out.println(data);
    }


}
